// pages/reimbursespList/reimbursespList.js

//获取应用实例
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		clientName: null,
		client: [
			// {
			// 	projectname: "6666元",
			// 	clientname: "akjdh1111hdkkjhask",
			// 	groupleader: "lkahdkdhaksjdhkja",
			// 	leader: "asdasdasd",
			// 	business: "asdasdasd",
			// 	staticc: 0,
			// },
			// {
			// 	projectname: "1234元",
			// 	clientname: "akjdha22222jhask",
			// 	groupleader: "lkahdkashkdhaksjdhkja",
			// 	leader: "asdasdasd",
			// 	business: "asdasdasd",
			// 	staticc: 1,
			// },
		],
		height: 0,
		aindex: 0,
	},
	// 提交
	  sbmt: function(e){
	    console.log(e.currentTarget.dataset.id);
	   var that=this;
	   wx.request({
	     url: app.globalData.apiUrl + '/wx/wxsubmitReimbursementDo', //接口地址
	     data: {
	       loginName: app.globalData.loginName,
	       status: '1',
	       Id:e.currentTarget.dataset.id
	     },
	     success: function (res) {
	       console.log( res)
	       wx.showToast({
	         title: '提交成功',
	         icon: 'success',
	         duration: 2000
	       })
	       that.onLoad();
	     }
	   })
	  },
	  // 撤回
	  recall: function(e){
	    console.log(e.currentTarget.dataset.id);
	    var that=this;
	    wx.request({
	      url: app.globalData.apiUrl + '/wx/wxsubmitReimbursementDo', //接口地址
	      data: {
	        loginName: app.globalData.loginName,
	        status: '0',
	        Id:e.currentTarget.dataset.id,
			approvalRoleCode: 'no',
	      },
	      success: function (res) {
	        console.log( res)
	        wx.showToast({
	          title: '撤回成功',
	          icon: 'success',
	          duration: 2000
	        })
	        that.onLoad();
	      }
	    })
	  },
	// tab 切换
	clicktab: function(e) {
		console.log(e);
		var that = this;
		if (this.data.aindex === e.target.dataset.current) {
			return false;
		} else {
			that.setData({
				aindex: e.target.dataset.current
			});
			if(this.data.aindex=='0'){
				console.log('我的报销单');
				that.getPageInfo();
			}
			if(this.data.aindex=='1'){
				console.log('待审批');
				that.getDsp();
			}
			if(this.data.aindex=='2'){
				console.log('已审批');
			}
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.getPageInfo();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	
	// 待审批
	getDsp: function() {
		var that = this;
		wx.request({
			url: app.globalData.apiUrl + '/wx/wxdepartmentApproveListData', //接口地址
			data: {
				loginName: app.globalData.loginName,
				pageNumber: 1,
				pageSize: 6,
				totalCount: 0,
				pageOrderName: 'r.opAt',
				pageOrderBy: 'descending',
			},
			method: 'POST',
			header: {
				'Content-Type': 'application/x-www-form-urlencoded' // 默认值
			},
			success(res) {
				// var myOldData=that.data.imageList;
				var mydata = res.data.data.list;
				that.setData({
					client: mydata,
				})
	
				if (mydata == null || mydata.length == 0) {
					wx.showToast({ //如果全部加载完成了也弹一个框
						title: '没有数据了',
						//image: "/images/warn.png",
						duration: 3000
					});
				}
	
			}
		})
		// 初始化图片列表  结束
		// 初始化 高度   
		wx.getSystemInfo({
			success: (res) => {
	
				that.setData({
					height: res.windowHeight
				})
			}
		})
	},

	getPageInfo: function() {
		var that = this;
		wx.request({
			url: app.globalData.apiUrl + '/wx/wxmyReimburseListData', //接口地址
			data: {
				loginName: app.globalData.loginName,
				pageNumber: 1,
				pageSize: 6,
				totalCount: 0,
				pageOrderName: 'r.sortTop DESC,r.opAt',
				pageOrderBy: 'descending',
			},
			method: 'POST',
			header: {
				'Content-Type': 'application/x-www-form-urlencoded' // 默认值
			},
			success(res) {
				// var myOldData=that.data.imageList;
				var mydata = res.data.data.list;
				that.setData({
					client: mydata,
				})

				if (mydata == null || mydata.length == 0) {
					wx.showToast({ //如果全部加载完成了也弹一个框
						title: '没有数据了',
						//image: "/images/warn.png",
						duration: 3000
					});
				}

			}
		})
		// 初始化图片列表  结束
		// 初始化 高度   
		wx.getSystemInfo({
			success: (res) => {

				that.setData({
					height: res.windowHeight
				})
			}
		})
	},

	lower: function() {
		var that = this;
		var result = that.data.client;
		var pagenumber = parseInt(result.length / 6 + 1);
		// 加载图片列表
		if (pagenumber < 2) {
			return false;
		} else {
			wx.request({
				url: app.globalData.apiUrl + '/wx/wxmyReimburseListData', //接口地址
				data: {
					loginName: app.globalData.loginName,
					pageNumber: pagenumber,
					pageSize: 6,
					totalCount: 0,
					pageOrderName: 'r.sortTop DESC,r.opAt',
					pageOrderBy: 'descending',
				},

				success(res) {

					if (result.length == res.data.totalCount) {
						wx.showToast({ //如果全部加载完成了也弹一个框
							title: '没有数据了',
							//image: "/images/warn.png",
							duration: 300
						});
						return false;
					}

					// var myOldData=that.data.imageList;
					var mydata = res.data.data.list;
					var cont = result.concat(mydata);
					if (mydata.length == 0) {
						wx.showToast({ //如果全部加载完成了也弹一个框
							title: '没有数据了',
							//image: "/images/warn.png",
							duration: 300
						});
						return false;
					}
					if (cont.length >= 100) {
						wx.showToast({ //如果全部加载完成了也弹一个框
							title: '加载的太多了',
							icon: 'success',
							duration: 300
						});
						return false;
					} else {
						wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中” 
							title: '加载中',
							icon: 'loading',
						});
						setTimeout(() => {
							that.setData({
								client: cont,
							});
							wx.hideLoading();
						}, 1500)
					}
				}
			})
		}
	},

})
